/**
 * @since 2011-4-19
 */

package com.ebrothers.isns.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.ebrothers.isns.common.Constants;

public class HttpManager {
	private static final String TAG = "HttpManager";
	// ===========================================================
	// Constants
	// ===========================================================
	private static final String SCHEME_HTTP = "http";
	private static DefaultHttpClient sClient;

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public synchronized static DefaultHttpClient getThreadSafeClient() {
		if (sClient == null) {
			final HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, "gbk");
			HttpConnectionParams.setStaleCheckingEnabled(params, false);
			HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
			HttpConnectionParams.setSoTimeout(params, 20 * 1000);
			HttpConnectionParams.setSocketBufferSize(params, 8192);
			HttpClientParams.setRedirecting(params, false);
			SchemeRegistry schemeRegistry = new SchemeRegistry();
			schemeRegistry.register(new Scheme(SCHEME_HTTP, PlainSocketFactory
					.getSocketFactory(), 80));
			ClientConnectionManager manager = new ThreadSafeClientConnManager(
					params, schemeRegistry);
			sClient = new DefaultHttpClient(manager, params);
		}
		return sClient;
	}

	public static HttpResponse execute(HttpGet get) throws IOException {
		return getThreadSafeClient().execute(get);
	}

	public static String getString(String url) throws IOException {
		if (Constants.LOGD) {
			Log.d(TAG, "****** getString. Get string from " + url);
		}
		final HttpGet httpGet = new HttpGet(url);
		String result = null;
		try {
			HttpResponse response = execute(httpGet);
			int responseCode = response.getStatusLine().getStatusCode();
			final HttpEntity entity = response.getEntity();
			if (HttpStatus.SC_OK == responseCode) {
				result = EntityUtils.toString(entity);
			} else {
				Log.e(TAG, "****** Error when get string. url=" + url + ", ["
						+ response.getStatusLine() + "]");
			}
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (IOException e) {
			httpGet.abort();
			throw e;
		}
		return result;
	}

	public static boolean tryToAccess(String url) {
		final HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = execute(httpGet);
			int responseCode = response.getStatusLine().getStatusCode();
			final HttpEntity entity = response.getEntity();
			if (entity != null) {
				entity.consumeContent();
			}
			if (HttpStatus.SC_OK == responseCode) {
				return true;
			}
		} catch (IOException e) {
			httpGet.abort();
		}
		return false;
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
